package com.dmap.admin.aop;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.LoginV2Param;
import com.dmap.admin.controller.backgroundv2.param.UserV2Param;
import com.dmap.admin.po.LoginLog;
import com.dmap.admin.service.backgroundv2.LoginLogService;
import com.dmap.admin.service.backgroundv2.UserV2Service;
import com.dmap.admin.vo.backgroundv2.LoginVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * \* User: helei
 * \* Date: 04/05/18
 * \* Description:
 * \
 */
@Aspect
@Component
public class LoginLogAop {

    @Resource
    private LoginLogService loginLogService;
    @Resource
    private UserV2Service userV2Service;

    @Pointcut("execution(public * com.dmap.admin.controller.backgroundv2.LoginV2Controller.login(..))")
    public void loginLog() {
        //Do nothing in this method.
    }

    @AfterReturning(pointcut = "loginLog()", returning = "object")
    public void doAfterReturning(JoinPoint joinPoint, Object object) {
        new Thread(() -> {
            BaseVo baseVo = (BaseVo) object;
            if (baseVo == null || joinPoint == null) {
                return;
            }
            LoginVo loginVo = (LoginVo) baseVo.getData();
            if (loginVo == null || StringUtils.isBlank(loginVo.getUserId()) || !StringUtils.isNumeric(loginVo.getUserId())
                    || Long.valueOf(loginVo.getUserId()).equals(0L)) {
                return;
            }
            //如果登陆成功，则在日志表中增加一条该用户的登陆记录
            if (baseVo.getCode().equals(Constant.RESPONSE_SUCCESS_CODE)) {
                Object[] arguments = joinPoint.getArgs();
                LoginV2Param loginV2Param = (LoginV2Param) arguments[0];
                LoginLog loginLog = new LoginLog();
                if (loginV2Param != null) {
                    loginLog.setUserAccount(loginV2Param.getAccount());
                    loginLog.setUserHandphone(loginV2Param.getHandphone());
                    loginLog.setIp(loginV2Param.getIp());
                    loginLog.setAddress(loginV2Param.getAddress());
                    loginLog.setBrowser(loginV2Param.getBrowser());
                }
                loginLog.setUserId(Long.valueOf(loginVo.getUserId()));
                loginLog.setCreateTime(System.currentTimeMillis() / 1000);
                loginLog.setUpdateTime(loginLog.getCreateTime());
                loginLogService.insertLoginLog(loginLog);

                //登陆成功的用户并且登陆错误次数大于0的，则将登陆错误次数置0
                if (loginVo.getLoginWrongNum() > 0) {
                    UserV2Param param = new UserV2Param();
                    param.setId(loginVo.getUserId());
                    param.setLoginWrongNum(0);
                    userV2Service.updateUser(param);
                }
                //如果登陆的是密码错误，则将登陆错误次数加1
            } else if (baseVo.getCode().equals(AdminConstant.RESPONSE_LOGIN_V2_PW_ERROR_CODE)) {
                UserV2Param param = new UserV2Param();
                Integer loginWrongNum = loginVo.getLoginWrongNum() + 1;
                param.setId(loginVo.getUserId());
                param.setLoginWrongNum(loginWrongNum);
                userV2Service.updateUser(param);
            }
        }).start();
    }

}